<?php
namespace app\Admin\controller;
use app\admin\controller\Base;
use think\Db;
use think\Session;
use app\Admin\model\Article as ArticleModel;
class Article extends Base
{
    public function lst()
    {
        // $list=ArticleModel::paginate(8);
        // $list=db('article')->alias('a')->join('cate c' , 'c.id=a.cateid')->field('a.id,a.title,a.pic,a.author,a.state,c.catename')->paginate(8);
        $list=ArticleModel::paginate(8);
        $this->assign('list', $list);
        return $this->fetch();
    }

            





	 public function add()
    {
    	if(request()->isPost()){

    		$data=[
    			'title'=>input('title'),
    			'author'=>input('author'),
                'desc'=>input('desc'),
                'keywords'=>input('keywords'),
                'content'=>input('content'),
                'cateid'=>input('cateid'),
                'time'=>input(),
               

                ];
                if(input('state')=='on'){
                    $data['state']=1;

                }
                if($_FILES['pic']['tmp_name']){
                    //获取表单上传文件
                    $file = request()->file('pic');
                    //移动到框架下的目录
                    $info = $file->move(ROOT_PATH . 'public'. DS .'uploads');
                    $data['pic']='/uploads/' . $info->getSaveName();
                }
                $validate = \think\Loader::validate('Article');
                if(!$validate->scene('add')->check($data)){
                    $this->error($validate->getError());
                    die;
                }
    		if(Db::name('Article')->insert($data)){
    			return $this->success("添加文章成功","lst");
    		}else{
    			return $this->error("添加文章失败！！！");
    		}
    		return;
    	}
        $cateres=db('cate')->select();
        $this->assign('cateres',$cateres);
        return $this->fetch();
    }


    public function edit(){
         $id=input('id');
        $articles=db('article')->find($id);
    
        
        if(request()->isPost()){
            $data=[
                'id'=>input('id'),
                'title'=>input('title'),
                'author'=>input('author'),
                'desc'=>input('desc'),
                'keywords'=>input('keywords'),
                'content'=>input('content'),
                'cateid'=>input('cateid'),
                'time'=>input(),
            ];

            if(input('state')=='on'){
                    $data['state']=1;

                }else{
                    $data['state']=0;
                }
                if($_FILES['pic']['tmp_name']){
                   // @unlink(SITE_URL.'/public/uploads'.$Article['pic']);
                   $picstr= SITE_URL.'/public'.$articles['pic']; 
                   @unlink($picstr);
                    //获取表单上传文件
                    $file = request()->file('pic');
                    //移动到框架下的目录
                    $info = $file->move(ROOT_PATH . 'public'. DS .'uploads');
                    $data['pic']='/uploads/' . $info->getSaveName();
                }
            $validate = \think\Loader::validate('Article');
                if(!$validate->scene('edit')->check($data)){
                    $this->error($validate->getError());
                    die;
                        }
            if(db('article')->update($data)){

                $this->success('修改成功','lst');
            }else{
                $this->error('修改失败');
            }
            return;
        }
       $this->assign('Articles',$articles);
               $cateres=db('cate')->select();
        $this->assign('cateres',$cateres);
        return $this->fetch();
    }

    public function del(){
        $id=input('id');
        if($id!=1){
            if(db('article')->delete(input('id'))){
                $this->success('删除文章成功！','lst');
            }else{
                $this->error('删除文章失败！');
            }
        }
    }







}

?>